package com.ftwj.demo.permission.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ftwj.demo.permission.dao.ResourceDao;
import com.ftwj.demo.permission.entity.ResourceBean;
import com.ftwj.demo.permission.service.ResourceService;
import com.ftwj.demo.permission.vo.RouterEntity;
import com.ftwj.demo.utils.convertor.ListToTreeUtil;

/**
*
* <p>Title: ResourceServiceImpl</p>  
* <p>Description: 权限管理业务实现类</p>
* <p>Copyright: Copyright (c) 2020 </p>  
* <p>Company: 兰州飞天网景信息产业有限公司</p>    
* @author WangSLi
* @date 2020年4月4日 下午5:58:46
* @version V1.0
*/
@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceDao, ResourceBean> implements ResourceService {

	@Autowired
	private ResourceDao dao;
	
	/**
	 * 	根据父ID获取资源树
	 */
	@Override
	public List<Map<String, Object>> getResourceTree(Integer pid, Integer type, Integer size) {
		List<Map<String, Object>> list = dao.selectResourceTree(pid,type,size);
		list = ListToTreeUtil.buildMapTree(list, "resourceId", pid.toString(), "pid", "children");
		return list;
	}
	
	/**
	 * 	根据角色ID获取其资源列表
	 */
	@Override
	public List<ResourceBean> getListByRoleId(Integer roleId) {
		return dao.selectListByRoleId(roleId);
	}

	/**
	 * 	分页列表
	 */
	@Override
	public IPage<Map<String, Object>> page(IPage<ResourceBean> page, ResourceBean bean) {
		QueryWrapper<ResourceBean> queryWrapper=new QueryWrapper<ResourceBean>(bean);
		IPage<Map<String, Object>> data = dao.selectMapsPage(page, queryWrapper);
		return data;
	}

	/**
	 * 	根据角色获取资源
	 */
	@Override
	public List<Integer> getResouceIdListByRoleId(Integer roleId,Integer type) {
		return dao.selectResouceIdListByRoleId(roleId,type);
	}

	/**
	 * 	获取页面路由表
	 */
	@Override
	public List<RouterEntity> getRouterByRoles(List<String> roles) {
		 List<RouterEntity> router = dao.selectRouterListByRoleId(roles);
		 router=ListToTreeUtil.buildBeanTree(router, RouterEntity.class, "id", "0", "pid", "children");
		 return router;
	}

}
